<?php
namespace app\admin\controller;
use catetree\Catetree;
class AuthGroup extends Common
{
    public function lst()
    {
        $groupRes=db('authGroup')->paginate(10);
        $count=db('authGroup')->count();
        $this->assign([
            'count'=>$count,
            'groupRes'=>$groupRes
        ]);
        return view();
    }

    public function add()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            $add=db('auth_group')->insert($data);
            if($add){
                return ['msg'=>'新增用户组成功','icon'=>1];
            }else{
                return ['msg'=>'新增用户组失败','icon'=>2];
            }
            return;
        }
        return view();
    }

    public function edit()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            $save=db('authGroup')->update($data);
            if($save !== false){
                return ['msg'=>'编辑用户组成功','icon'=>1];
            }else{
                return ['msg'=>'编辑用户组失败','icon'=>2];
            }
            return;
        }
        $groups=db('authGroup')->find(input('id'));
        $this->assign('groups',$groups);
        return view();
    }

    public function del($id)
    {
        if($id == 1){
            return json(['msg'=>'超级管理员用户组不允许删除','icon'=>2]);
        }else{
            //删除用户组下所有管理员
            $delAdmin=db('admin')->where(['group_id'=>$id])->delete();
            //删除用户组关联表所有关联
            $delGroupAccess=db('auth_group_access')->where(['group_id'=>$id])->delete();
            //删除用户组
            $del=db('auth_group')->delete($id);
            if($del){
                return ['msg'=>'删除用户组成功','icon'=>1];
            }else{
                return ['msg'=>'删除用户组失败','icon'=>2];
            }
        }
    }
    //批量删除
    public function delAll($data)
    {   
        $data= array_filter($data);
        foreach ($data as $k => $v){
            //删除用户组下所有管理员
            $delAdmin=db('admin')->where(['group_id'=>$v])->delete();
            //删除用户组关联表所有关联
            $delGroupAccess=db('auth_group_access')->where(['group_id'=>$v])->delete();
            //删除用户组
            $del=db('auth_group')->delete($v);
        }
        return ['msg'=>'批量删除管理员成功','icon'=>1];
    }
    
    //ajax更新开启状态
    public function changeStatus()
    {
        if(request()->isAjax()){
            $id=input('id');
            $status=input('status');
            if($status == 1){
                db('auth_group')->where(['id'=>$id])->update(['status'=>0]);
                return ['msg'=>'已禁用','icon'=>2];
            }else{
                db('auth_group')->where(['id'=>$id])->update(['status'=>1]);
                return ['msg'=>'已启用','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }

    //用户组权限
    public function power(){
        if(request()->isAjax()){
            $data=input('post.');
            if(isset($data['rule'])){
                $rules=implode(',', $data['rule']);
                $save=db('authGroup')->where('id',$data['id'])->update(['rules'=>$rules]);
            }else{
                $save=db('authGroup')->where('id',$data['id'])->update(['rules'=>'']);
            }
            if($save !== false){
                return ['msg'=>'权限分配成功','icon'=>1];
            }else{
                return ['msg'=>'权限分配失败','icon'=>2];
            }
            return;
        }

        //获取顶级分类的规则
        $ruleRes=db('authRule')->where(['pid'=>0])->order('sort')->select();
        foreach ($ruleRes as $k => $v) {
            //获取二级分类的规则
            $ruleRes[$k]['children']=db('authRule')->where(['pid'=>$v['id']])->order('sort')->select();
            //获取三级分类的规则
            foreach ($ruleRes[$k]['children'] as $k1 => $v1) {
                $ruleRes[$k]['children'][$k1]['children']=db('authRule')->where(['pid'=>$v1['id']])->order('sort')->select();
            }
        }
        // $_ruleRes=db('auth_rule')->order('sort')->select();
        // $CateTree=new Catetree();
        // $ruleRes=$CateTree->catetree($_ruleRes);
        $id=input('id');
        $groups=db('authGroup')->find($id);
        // dump($ruleRes);die;
        $rules=explode(',',$groups['rules']);
        $this->assign([
            'groups'=>$groups,
            'ruleRes'=>$ruleRes,
            'rules'=>$rules,
        ]);
        return view();
    }








}
